package com.gamemost.templestay;

import com.gamemost.layout.LayoutViewReadBook;
import com.gamemost.templestay.R;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.view.Window;

public class PreferenceSettingInScreen extends PreferenceActivity implements OnSharedPreferenceChangeListener{
	public static boolean fixLayout = false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);	
//		addPreferencesFromResource(R.xml.setting_screen);
		if(!fixLayout) addPreferencesFromResource(R.xml.setting_screen);
		else addPreferencesFromResource(R.xml.setting_screen_fix);
	   PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences pref, String key) {
		// TODO Auto-generated method stub
		System.out.println("settingcheck "+LayoutViewReadBook.settingChange);
		if (key.equals(getResources().getString(R.string.pref_user_color))) {
			if (!LayoutViewReadBook.settingChange) {
			LayoutViewReadBook.settingChange = true;
			Config.colorChange = true;
			String color = pref.getString(key, getResources().getString(R.string.WHITE));
			int codeColor;
			if (getResources().getString(R.string.YELLOWMILK).equals(color)) {
				codeColor = getResources().getColor(R.color.yellowMilk);
				Config.color = "#f8f2da";
			} else if (getResources().getString(R.string.BLACK).equals(color)) {
				codeColor = getResources().getColor(R.color.black);
				Config.color = "#000000";
			} else {
				codeColor = getResources().getColor(R.color.white);
				Config.color = "#ffffff";
			}
			Config.codeColor = codeColor;
			System.out.println("set color start"+codeColor);
			if (fixLayout) { 
				LayoutViewReadBook.getInstance().getHtml5WebView().setBackgroundColor(codeColor);			
			}
			System.out.println("set color now");
			LayoutViewReadBook.getInstance().reload();
			System.out.println("set color end");
			}
		} else if (key.equals(getResources().getString(R.string.pref_user_font))) {
			if (!LayoutViewReadBook.settingChange) {
			LayoutViewReadBook.settingChange = true;
			String font = pref.getString(key, Config.font);
			Config.font = font;
			System.out.println("set font now");
			LayoutViewReadBook.getInstance().reload();
			System.out.println("set font end");
			}

		} else if (key.equals(getResources().getString(R.string.pref_user_textsize))) {
			Config.fontSize = pref.getString(key, getResources().getString(R.string.MEIDUM));
			LayoutViewReadBook.getInstance().changeFontSize(Integer.parseInt(Config.fontSize));
			LayoutViewReadBook.getInstance().loadSinglePage(LayoutViewReadBook.currentPage);
		}
		finish();
	}	
}

